﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Component;

public partial class BackEnd_ProductList : System.Web.UI.UserControl
{
    public string _Session = "";
    ArrayList al = new ArrayList();
    public string _Url = BackEndQuery._AdminQuery + Component.BackEndQuery._ProductM + "&" + ConstString._Action + "=" + Component.ConstString.Add;
    int _IdSubCat = 0;
     protected void Page_Load(object sender, EventArgs e)
    {
       
        _IdSubCat = ConvertType.ToInt(QueryHelper.GetQueryString(Request, ConstString._idsubcat));
        _Url += "&" + ConstString._idsubcat + "=" + _IdSubCat;
        _Session = ConvertType.ToString(Sessions.CatSub) + " --> <a href=\"" + Request.RawUrl + "\" >" + CategorySubDB.GetObjectByid(_IdSubCat, true).Name + "</a>";
        Sessions.UrlAdmin = Request.Url.ToString();
        DataBind(ProductDB.GetAllObjByIdCatSub(_IdSubCat,true));
        grid.RowCancelingEdit += delegate {  Response.Redirect(Request.Url.ToString()); };
    }
  
    #region ----------------------------- Ham viet tay -------------------------------------
    void DataBind(object al)
    {
        grid.DataKeyNames = new string[] { ColumProduct._Id};
        grid.DataSource = al;
        grid.DataBind();
    }
   
    #endregion
    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        object id = grid.DataKeys[e.RowIndex].Value;      
        DropDownList ddlAct = (DropDownList)grid.Rows[e.RowIndex].FindControl("ddlAct");
        DropDownList ddlNew = (DropDownList)grid.Rows[e.RowIndex].FindControl("ddlNew");
        DropDownList ddlCatSub = (DropDownList)grid.Rows[e.RowIndex].FindControl("ddlCatSub");
        DropDownList ddlBestSeller = (DropDownList)grid.Rows[e.RowIndex].FindControl("ddlBestSeller");
        DropDownList ddlSpecial = (DropDownList)grid.Rows[e.RowIndex].FindControl("ddlSpecial");
        TextBox txtName = (TextBox)grid.Rows[e.RowIndex].FindControl("txtName");
        TextBox txtNameEL = (TextBox)grid.Rows[e.RowIndex].FindControl("txtNameEL");
        Product pro = ProductDB.GetObjectByid(ConvertType.ToInt(id),true);      
        pro.Active = ConvertType.ToBool(ddlAct.Text);
        pro.New = ConvertType.ToBool(ddlNew.Text);
        pro.Special = ConvertType.ToBool(ddlSpecial.Text);
        pro.BestSeller = ConvertType.ToBool(ddlBestSeller.Text);
        pro.Name = txtName.Text.Trim();
        pro.NameEL = txtNameEL.Text.Trim();
        if (ProductDB.Save(pro) == 1)
        {
            this.labMessage.Text=ConstString.chenTC;
        }
         Response.Redirect(Request.Url.ToString());
    }
    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid.EditIndex = e.NewEditIndex;
        DataBind(ProductDB.GetAllObjByIdCatSub(_IdSubCat, true));
    }
    //protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    try
    //    {
    //        object id = grid.DataKeys[e.RowIndex].Value;
    //        Product Product = ProductDB.GetObjectByid(ConvertType.ToInt(id),true);      
    //              if (ProductDB.Delete(Product) == 1)
    //                  this.labMessage.Text = ConstString.xoaTC;
    //    }
    //    catch { }
    //    DataBind(ProductDB.GetAll(true));
    //}

    protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grid.PageIndex = e.NewPageIndex;
        DataBind(ProductDB.GetAllObjByIdCatSub(_IdSubCat, true));
    }
    protected void Delete(object sender, EventArgs e)
    {
        foreach (string key in Request.Form.AllKeys)
        {
            if (key.Contains("check") == true)
            {
                Product pro = Component.ProductDB.GetObjectByid(ConvertType.ToInt(key.Replace("check", "")), true);
                if (pro != null)
                {
                    Component.ProductDB.Delete(pro);
                }
            }
        }
        DataBind(ProductDB.GetAllObjByIdCatSub(_IdSubCat, true));
    }
}
